package com.yayan.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import com.alibaba.fastjson.JSONObject;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.request.DelFileRequest;
import com.qcloud.cos.request.StatFileRequest;
import com.qcloud.cos.request.UploadFileRequest;
import com.qcloud.cos.sign.Credentials;

public class TenXunYunUtil {
	private static Properties prop = null;
	private static String txyAppId = null;
	private static String txySecretId = null;
	private static String txySecretKey = null;
	private static String txyBucketName = null;
	private static String newsVideoFilePath = null;
	private static String reviewVideoFilePath = null;
	private static String reviewImagesFilePath = null;
	private static String matchImagesFilePath = null;
	private static String bannerImagesFilePath = null;
	private static String region = "gz";
	
	static{
		InputStream in = null;
		prop = new Properties();
		try{
			in = FilePathUtil.class.getClassLoader().getResourceAsStream("upload.properties");
			prop.load(in);
			txyAppId = prop.getProperty("txyAppId");
			txySecretId = prop.getProperty("txySecretId");
			txySecretKey = prop.getProperty("txySecretKey");
			txyBucketName = prop.getProperty("txyBucketName");
			newsVideoFilePath = prop.getProperty("newsVideoFilePath");
			reviewVideoFilePath = prop.getProperty("reviewVideoFilePath");
			reviewImagesFilePath = prop.getProperty("reviewImagesFilePath");
			matchImagesFilePath = prop.getProperty("matchImagesFilePath");
			bannerImagesFilePath = prop.getProperty("bannerImagesFilePath");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(in != null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 上传文件 并返回路径
	 * @param fileName 文件名
	 * @param type 类型  0赛事图片 1回顾图片 2回顾视频 3新闻视频 4广告图片
	 * @param fileUrl 文件本地路径
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String upFile(String fileName, int type, String fileUrl) {
		
		Credentials cred = new Credentials(Long.parseLong(txyAppId), txySecretId, txySecretKey);
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.setRegion(region);
        COSClient cosClient = new COSClient(clientConfig, cred);
        
        String filePath = null;
        switch (type) {
		case 0:
			filePath = matchImagesFilePath + "/";
			break;
		case 1:
			filePath = reviewImagesFilePath + "/";
			break;
		case 2:
			filePath = reviewVideoFilePath + "/";
			break;
		case 3:
			filePath = newsVideoFilePath + "/";
			break;
		case 4:
			filePath = bannerImagesFilePath + "/";
			break;
		}
        
        //上传腾讯云
        UploadFileRequest fileRequest = new UploadFileRequest(txyBucketName, filePath + fileName, fileUrl + fileName);
        cosClient.uploadFile(fileRequest);
        
		//获取上传后文件路径
		StatFileRequest statFileRequest = new StatFileRequest(txyBucketName, filePath + fileName);
		String url = cosClient.statFile(statFileRequest);
		
		Map<String, Object> jsmap = JSONObject.parseObject(url);
		Map<String, Object> datamap = (Map<String, Object>) jsmap.get("data");
		url = (String) datamap.get("source_url");
		
		return url;
	}
	
	/**
	 * 删除文件
	 * @param fileUrl
	 */
	public static void delFile(String fileUrl) {
		Credentials cred = new Credentials(Long.parseLong(txyAppId), txySecretId, txySecretKey);
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.setRegion(region);
        COSClient cosClient = new COSClient(clientConfig, cred);
        
        String[] strarr = fileUrl.split("/");
        DelFileRequest fileRequest = new DelFileRequest(txyBucketName, "/" + strarr[3] + "/" + strarr[4]);
        
        cosClient.delFile(fileRequest);
        
	}
}
